<?php
namespace App\Controllers\Index;
use App\Models\Experience\ExpOrder;
use App\Libs\Helper\CryptHelper;
/**
*	验证支付二维码是否正确体验 
*/
class QrcodeOrderDeEx extends \App\Libs\Base\BaseAction
{
    protected function beforeRun() {
        parent::beforeRun();
        $this->needLogin = TRUE;
    }
	
	public function execute(){
		//商户消费返回
		$qrcodeBackResult = $this->request->get('backResult');
		if( empty( $qrcodeBackResult ) ){
			return \App\Libs\Views\Json::view(1, '商户消费返回为空', null);	
		}
		
		//反解析结果值
		$result = CryptHelper::decryptBase64( $qrcodeBackResult );
		$resultInfo = explode('_',$result);
		if( empty( $resultInfo ) ){
			return \App\Libs\Views\Json::view(1, '商户消费返回为空', null);		
		}
		
		//查询时间戳是否过期
		$qrcodeInvTime = \App\Libs\Base\Config::instance()->common['qrcodeInvTime'];
		$invTime = strtotime($qrcodeInvTime);//过期时间
		if( $invTime > $resultInfo[0] ){
			return \App\Libs\Views\Json::view(1, '时间已过期，请刷新二维码！', null);		
		}
		
		//检查订单是否存在
		$order = ExpOrder::find(array(
			' order_sn = :orderSn: and status = 2 and uid = :uid:',
			'bind' => array( 'orderSn'=>$resultInfo[2] , 'uid'=>$resultInfo[1] )
		))->toArray();
		if( empty( $order ) ){
			return \App\Libs\Views\Json::view(1, '订单不存在', null);	
		}
		
		//是否需要修改订单消费状态？
		$order->status = 4;
		if( $order->save() == false ){
			return \App\Libs\Views\Json::view(1, '失败', null);		
		}else{
			return \App\Libs\Views\Json::view(0, '消费成功', null);			
		}
		
		
	}
}
